/**
 * zhangguoqing
 */
$(document).ready(function() {
	$("#tab1").hide(); // Initially hide all content
	$("#tab2").hide();// Initially hide all content
	$("#tabs li:first").attr("id", "current"); // Activate first tab
	$("#tab1").fadeIn(); // Show first tab content

	$('#tabs a').click(function(e) {
		e.preventDefault();
		$("#tab1").hide();// Hide all content
		$("#tab2").hide();// Hide all content
		$("#tabs li").attr("id", ""); // Reset id's
		$(this).parent().attr("id", "current"); // Activate this
		$('#' + $(this).attr('name')).fadeIn(); // Show content for current tab
	});
	clickGetDsmData();
	commitDSMData();
});
/**
 * 点击获取DSM数据信息
 * */
function clickGetDsmData() {
	$(document).on("click", "#clickGetData", function() {
		var appNameVal = $("#appNamesId").val();
		if (!appNameVal) {
			swal('失败', '请填写appName名称', 'warning');
			return false;
		}
		var url = getRoot() + '/datasource/getDSMDatasourceInfo';
		var params = {};
		params.appName = $("#appNamesId").val();
		$.post(url, params, function(data) {
			if(data.success){
				var dsmArray = data.root;
				var str = "<option data-tokens='请选择数据源' value=''>请选择数据源</option>";
				for(var i in dsmArray){
					var obj = dsmArray[i];
						str += "<option class='dbDSM-opt' value='"
							+obj.id
							+"' data-tokens='"+obj.dbType+"@"+obj.dbIp+":"+obj.dbPort+"@"+obj.dbUser+"@"+obj.dbName
							+"'>"+obj.dbType+"@"+obj.dbIp+":"+obj.dbPort+"@"+obj.dbUser+"@"+obj.dbName
							+"</option>";
				}
				$("#dsmDatasourceId").empty();
				$("#dsmDatasourceId").append(str);
				$("#dsmDatasourceId").selectpicker('refresh');
			}else{
				if(data.failure){
					swal('失败',data.root, 'error');
					return false;
				}
			}
		});
	});
}
/**
 * 提交DSM数据
 * */
function commitDSMData(){
	$(document).on("click","#commitDSMBtn",function(){
		if(!$("#dsmDatasourceId").val()){
			swal('警告','请选择数据源','warning');
			return false;
		}
		if(!$("#projectDSMId").val()){
			swal('警告','您没有选择项目组,无法提交','warning');
			return false;
		}
		var appNameVal = $("#appNamesId").val();
		if (!appNameVal) {
			swal('失败', '请填写appName名称', 'warning');
			return false;
		}
		var params = {};
		params['id'] = $("#dsmDatasourceId").val();
		params['appName'] = appNameVal;
		params['projectName'] = $("#projectDSMId").val();
		var url = getRoot() + '/datasource/insertDSMDatasource';
		$.post(url,params,function(data){
			if(data.success){
				swal('成功',data.root,'success');
			}else{
				swal('失败',data.root,'error');
			}
		});
	});
}